package com.tsingteng.cosfun.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.HomeBean;
import com.tsingteng.cosfun.bean.OpusBean;

//评论选择回复或举报
public class LongPressShowDialog extends Dialog {
    public LongPressShowDialog(Context context) {
        super(context);
    }

    public LongPressShowDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private OnItemClickListener onItemClickListener;

        private View layout;
        private LongPressShowDialog dialog;
        private int position;
        private int type; //是否是删除
        private OpusBean opusBean;

        public Builder(Context context, Activity activity) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new LongPressShowDialog(context, R.style.commselectDialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.long_press_dialog, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }

        public Builder setItemClickListener(OnItemClickListener listener) {
            this.onItemClickListener = listener;
            return this;
        }

        public Builder setMessage(int position, OpusBean opusBean, int type) {
            this.opusBean = opusBean;
            this.position = position;
            this.type = type;
            return this;
        }

        /**
         * 创建单按钮对话框
         *
         * @return
         */
        public LongPressShowDialog createDialog() {
            TextView unlike = layout.findViewById(R.id.long_press_unlike);
            TextView jubao = layout.findViewById(R.id.long_press_jubao);
            TextView del = layout.findViewById(R.id.long_press_del);
            unlike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onUnlike(position, opusBean);
                        dialog.dismiss();
                    }
                }
            });
            jubao.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onReport(position, opusBean);
                        dialog.dismiss();
                    }
                }
            });
            del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onDel(position, opusBean);
                        dialog.dismiss();
                    }
                }
            });
            if (type == 1) {
                unlike.setVisibility(View.GONE);
                jubao.setVisibility(View.GONE);
                del.setVisibility(View.VISIBLE);
            } else {
                unlike.setVisibility(View.VISIBLE);
                jubao.setVisibility(View.VISIBLE);
                del.setVisibility(View.GONE);
            }
            create();
            return dialog;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private void create() {
            dialog.setContentView(layout);
            dialog.setCancelable(true);     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(true);        //用户不能通过点击对话框之外的地方取消对话框显示
        }

    }


    public interface OnItemClickListener {
        void onUnlike(int position, OpusBean opusBean);

        void onReport(int position, OpusBean opusBean);

        void onDel(int position, OpusBean opusBean);
    }
}
